<?php
namespace app\controller;

use \app\model\fileModel;
use \core\lib\session;
use \core\view\view;

class fileController{
	private $model;
	public function __construct(){
		$this->model = new fileModel();
		$user = session::get("user");
		if($user == false){
			jump("/user/login");
			exit(0);
		}
	}
	
	public function add(){
		$user = session::get("user");
		$name = getvalue("post", "auth");
		$typeid = getvalue("post", "typeId");
		$data = getvalue("post", "NoteData");
		$compressdata = getvalue("post", "CompressData");
		$user = session::get("user");
		$ok = $this->model->addFile($name, $data, $compressdata, $typeid, $user['id']);
		echo $ok ? "OK" : "ERROR";
	}
	
	public function view(){
		$id = getvalue("get", "id");
		$file = $this->model->loadFileById($id);
		view::assign("file", $file);
		view::display("file/NoteView.html");
	}
	
	public function search(){
		$name = getvalue("get", "key");
		$files = $this->model->loadFileByName($name);
		
		view::assign("files", $files);
		view::display("user/collection.html");
	}
	
	public function delete(){
		$id = getvalue("get", "id");
		$ok = $this->model->deleteFile($id);
		jump("/user/collection");
	}
	
	public function update(){
		$id = getvalue("get", "id");
		$file = $this->model->loadFileById($id);
		view::assign("file", $file);
		view::display("file/NoteUpdate.html");
	}
	
	public function updateFileById(){
		$user = session::get("user");
		$name = getvalue("post", "auth");
		$typeid = getvalue("post", "typeId");
		$data = getvalue("post", "NoteData");
		$compressdata = getvalue("post", "CompressData");
		$user = session::get("user");
		$id = getvalue("post", "collectid");
		$ok = $this->model->updateFile($data, $compressdata, $id);
		echo $ok ? "OK" : "ERROR";
	}
	
	public function loadFileByType(){
		$id = getvalue("get", "id");
		$res = $this->model->loadFileByType($id);
		echo json_encode($res);
	}
	
	public function loadFileByDate(){
		$user = session::get("user");
		$res = $this->model->loadFileByUser($user['id']);
		return json_encode($res);
	}
	
	public function loadtype(){
		$user = session::get("user");
		$res = $this->model->loadType($user['id']);
		echo json_encode($res);
	}
	
	public function addtype(){
		$user = session::get("user");
		$typename = getvalue("post", "typename");
		//p($typename);
		$ok = $this->model->addType($typename, $user['id']);
		echo $ok == false ? "ERROR" : "OK";
	}
	
	public function loadimage(){
		$id = getvalue("get", "id");
		$file = $this->model->loadFileById($id);
		echo json_encode($file);
	}

}
